package com.yong.sbd.biz.mybatis.controller;

import com.yong.sbd.biz.mybatis.entity.SysRole;
import com.yong.sbd.biz.mybatis.service.SysRoleService;
import com.yong.sbd.common.beans.response.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 角色控制器
 *
 * @author Kang Yong
 * @date 2025/10/24
 * @since 1.0.0
 */
@RestController
@RequestMapping("/role")
public class SysRoleController {

    @Autowired
    private SysRoleService roleService;

    /**
     * 根据ID查询角色
     *
     * @param id {@link Long}
     * @return {@link R<SysRole>}
     */
    @GetMapping("/{id}")
    public R<SysRole> getRoleById(@PathVariable Long id) {
        return R.success(roleService.getRoleById(id));
    }

    /**
     * 根据角色编码查询角色
     *
     * @param roleCode {@link String}
     * @return {@link R<SysRole>}
     */
    @GetMapping("/code/{roleCode}")
    public R<SysRole> getRoleByRoleCode(@PathVariable String roleCode) {
        return R.success(roleService.getRoleByRoleCode(roleCode));
    }

    /**
     * 查询所有角色
     *
     * @return {@link R<List<SysRole>>}
     */
    @GetMapping
    public R<List<SysRole>> getAllRoles() {
        return R.success(roleService.getAllRoles());
    }

    /**
     * 保存角色
     *
     * @param role {@link SysRole}
     * @return {@link R<Boolean>}
     */
    @PostMapping
    public R<Boolean> saveRole(@RequestBody SysRole role) {
        return R.success(roleService.saveRole(role));
    }

    /**
     * 更新角色
     *
     * @param role {@link SysRole}
     * @return {@link R<Boolean>}
     */
    @PutMapping
    public R<Boolean> updateRole(@RequestBody SysRole role) {
        return R.success(roleService.updateRole(role));
    }

    /**
     * 根据ID删除角色
     *
     * @param id {@link Long}
     * @return {@link R<Boolean>}
     */
    @DeleteMapping("/{id}")
    public R<Boolean> deleteRole(@PathVariable Long id) {
        return R.success(roleService.deleteRole(id));
    }
}